package com.iloomo.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

/**
 * 说明：参数封装Map
 * 创建人：loomo
 * 修改时间：2014年9月20日
 */
public class PageData extends HashMap implements Map {

    private static final long serialVersionUID = 1L;

    public List<Map<String, String>> getBatchParam() {
        return batchParam;
    }

    public void setBatchParam(List<Map<String, String>> batchParam) {
        this.batchParam = batchParam;
    }

    public List<Map<String, String>> getChildParams() {
        return childParams;
    }

    public void setChildParams(List<Map<String, String>> childParams) {
        this.childParams = childParams;
    }

    List<Map<String, String>> childParams = null;

    List<Map<String, String>> batchParam = null;
    Map map = null;
    HttpServletRequest request;

    public PageData(HttpServletRequest request) {
        this.request = request;
        Map properties = request.getParameterMap();

        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Entry entry;
        String name = "";
        String value = "";
       String business_id = request.getParameter("business_id");
        while (entries.hasNext()) {
            entry = (Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
            }

            if (name.equals("offset") || name.equals("pageSize")) {
                if (!value.equals("")) {
                    int num = Integer.valueOf(value);
                    returnMap.put(name, num);
                }
            } else if ("batch".equals(name)) {
                batchParam = dealBathJsonParam(value,business_id);
            }  else if ("subParams".equals(name)) {
                childParams = dealChildParams(value,business_id);
            } else {
                returnMap.put(name, value);
            }
        }
        map = returnMap;
    }

    public List<Map<String, String>> dealBathJsonParam(String jsonString,String business_id) {
        List<Map<String, String>> list = new ArrayList<>();
        JsonUtil jsonUtil = new JsonUtil();
        JSONArray jay = JSONArray.fromObject(jsonString);
        for(int i =0;i<jay.size();i++){
            Map<String, String> parameterMaps =jsonUtil.jsonObjToMap((JSONObject) jay.get(i));
            parameterMaps.put("business_id",business_id);
            list.add(parameterMaps);
        }
        return list;
    }
    public List<Map<String, String>> dealChildParams(String jsonString,String business_id) {
        List<Map<String, String>> list = new ArrayList<>();
        JsonUtil jsonUtil = new JsonUtil();
        JSONArray jay = JSONArray.fromObject(jsonString);
        for(int i =0;i<jay.size();i++){
            Map<String, String> parameterMaps =jsonUtil.jsonObjToMap((JSONObject) jay.get(i));
            parameterMaps.put("business_id",business_id);
            list.add(parameterMaps);
        }
        return list;
    }

    public PageData() {
        map = new HashMap();
    }

    @Override
    public Object get(Object key) {
        Object obj = null;
        if (map.get(key) instanceof Object[]) {
            Object[] arr = (Object[]) map.get(key);
            obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]);
        } else {
            obj = map.get(key);
        }
        return obj;
    }

    public String getString(Object key) {
        String string = null;
        if (get(key) != null) {
            string = get(key).toString();
        }
        return string;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object put(Object key, Object value) {
        return map.put(key, value);
    }

    @Override
    public Object remove(Object key) {
        return map.remove(key);
    }

    public void clear() {
        map.clear();
    }

    public boolean containsKey(Object key) {
        // TODO Auto-generated method stub
        return map.containsKey(key);
    }

    public boolean containsValue(Object value) {
        // TODO Auto-generated method stub
        return map.containsValue(value);
    }

    public Set entrySet() {
        // TODO Auto-generated method stub
        return map.entrySet();
    }

    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return map.isEmpty();
    }

    public Set keySet() {
        // TODO Auto-generated method stub
        return map.keySet();
    }

    @SuppressWarnings("unchecked")
    public void putAll(Map t) {
        // TODO Auto-generated method stub
        map.putAll(t);
    }

    public int size() {
        // TODO Auto-generated method stub
        return map.size();
    }

    public Collection values() {
        // TODO Auto-generated method stub
        return map.values();
    }

}
